/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.table.AbstractTableModel;

import com.ruipengkj.bean.TCommodity;

/**
 * 说明：下单Modle
 * 
 * @Author：李智深
 * @Date：2012-03-14
 */
public class OrderAddModel extends AbstractTableModel{

	private static final long serialVersionUID = 1L;
	private Vector<String> columns = new Vector<String>();
	private List<TCommodity> tCommodityList=new ArrayList<TCommodity>();
	
//	private List<JPanel> panelList;//表格总的面板
	
	public OrderAddModel(){
		columns.add("商品名称");
		columns.add("单价");
		columns.add("数量");
//		columns.add("售价");
		columns.add("折扣");
	}
	
	//获得行数
	@Override
	public int getRowCount(){
		return tCommodityList.size();
	}
	
	//获得列数
	@Override
	public int getColumnCount(){
		return columns.size();
	}
	
	//获得单元格的值
	public Object getValueAt(int row,int column){
		TCommodity p = (TCommodity) tCommodityList.get(row);
		return getTCommodityValueByCol(p, row, column);
	}
	
	public Object getTCommodityValueByCol(TCommodity tCommodity, int row, int columnIndex) {
		switch (columnIndex) {
			case 0://名称
				return tCommodity.getName();
			case 1://单价
				return tCommodity.getDiscountPrice();
			case 2://数量.
//				if (panelList != null && panelList.size() > row)
//					return panelList.get(row);
				if (tCommodity.getPanel() != null)
					return tCommodity.getPanel();
				return "";
//			case 3://售价
//				DecimalFormat df  = new DecimalFormat("###.0");
//				return df.format(tCommodity.getNum()*tCommodity.getDiscountPrice());
			case 3://折扣
				return tCommodity.getDisCount();
		}
		return null;
	}
	
	//获取列对应的类型
	public Class<?> getColumnClass(int column){
		if(column==2){
			return JPanel.class;
		} 
		return Object.class;
	}

	@Override
	public void setValueAt(Object aValue, int row, int columnIndex) {
		if (columnIndex == 2 && aValue != null && !aValue.equals("")) {
			tCommodityList.get(row).setNum(Integer.parseInt(aValue.toString()));
			fireTableCellUpdated(row, 3);
			return;
		}
		super.setValueAt(aValue, row, columnIndex);
	}

	//获取列名
	@Override
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	
	//设置单元格是否可编辑
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){
		if(columnIndex == 2){
			return true;
		}
		return false;
	}
	
	public void addList(TCommodity p) {
		tCommodityList.add(p);
		fireTableRowsInserted(tCommodityList.size()-1, tCommodityList.size()-1);
	}
	
	public boolean deleteList(int index) {
		if (index >= 0 && index < tCommodityList.size()) {
			tCommodityList.remove(index);
			fireTableRowsDeleted(index, index);
			return true;

		} else
			return false;
	}
	
	
	public List<TCommodity> getTCommodityList() {
		return tCommodityList;
	}
	
	public void setRowData(List<TCommodity> tCommodityList) {
		this.tCommodityList = tCommodityList;
	}
	
//	public void setListPanel(List<JPanel> panelList) {
//		this.panelList = panelList;
//	}
}
